package com.mooc.kafka.future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author StarsOfFuture_xYang
 * @version 1.0
 * @date 2021-07-07 11:02 上午
 * @information kafka-study - com.mooc.kafka.future
 **/
public class CTFTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main: >>"+Thread.currentThread().getName());
        System.err.println("小杨坐车来到昆仑国际");
        CompletableFuture<Void> future = CompletableFuture.supplyAsync(
                () -> "hr1接待了你并说了句欢迎您")
                .thenCombineAsync(CompletableFuture
                                .supplyAsync(
                                        () -> "hr2去倒了杯水并给了你"),
                        (result1, result2) -> result1 + " | " + result2)
                .thenComposeAsync((res) -> CompletableFuture.supplyAsync(() -> {
                    System.out.println("completableFuture1: >>"+Thread.currentThread().getName());
                    System.err.println("客气流程结束>>>>> " + res);
                    return "开始面试";
                })).thenApplyAsync((res) -> {
                    System.out.println("completableFuture2: >>"+Thread.currentThread().getName());
                    System.err.println(res);
                    try {
                        Thread.sleep(11);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    return "面试官1开始出题,问道:'线程的状态'";
                })
                .applyToEitherAsync(CompletableFuture
                        .supplyAsync(() -> "面试官2开始出题,问道:'你对jvm的理解'"), (res) -> {
                    System.out.println(res);
                    return res;
                })
                .exceptionally((ex) -> "出错了: " + ex.getMessage()).thenAcceptAsync((result) -> {
                    System.out.println("completableFuture3: >>"+Thread.currentThread().getName());
                    System.out.println("执行结果: " + result);
                });
//        System.out.println(future.get());
        if (future.isDone()||future.isCancelled()) {
            System.err.println(Thread.currentThread().getName()+" GGGGGG");
        }
    }
}
